﻿layui.use(['form', 'layer', 'table', 'laytpl', 'laydate'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;

    function loadDate() {
        laydate.render({
            elem: '#startdate'
        });
        laydate.render({
            elem: '#enddate'
        });
    }
    
    var tableIns = table.render({
        elem: '#managerList',
        url: '/InOutCash/LoadOfflineInCashRecordData/',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "managerListTable",
        cols: [[
            { type: "checkbox", fixed: "left", width: 50 },
            { field: "Id", title: '序号', width: 70, align: "center" },
            { field: 'ManagerRealName', title: '收卡名', minWidth: 50, align: "center", totalRowText: '合计:' },
            { field: 'ManagerCardNo', title: '收卡号', minWidth: 50, align: "center" },
            { field: 'OrderNo', title: '订单号', minWidth: 50, align: "center" },
            { field: 'Money', title: '金额(美元)', minWidth: 80, align: "center", totalRow: true },
            { field: 'ExchangeRate', title: '汇率', width: 70, align: "center" },
            { field: 'RMBMoney', title: '金额(RMB)', minWidth: 80, align: "center", totalRow: true },
            { field: 'UserBankProvince', title: '打款卡开户行', minWidth: 50, align: "center" },
            { field: 'UserBankType', title: '打款行', minWidth: 50, align: "center" },
            { field: 'UserCardNo', title: '打款卡号', minWidth: 80, align: "center" },
            { field: 'UserName', title: '交易号(姓名)', minWidth: 150, align: "center", templet: '<span>{{d.UserName}}({{d.UserRealName}})</span>' },
            //{ field: 'AuditTime', title: '审核时间', minWidth: 80, align: "center" },
            { field: 'AddTime', title: '申请时间', minWidth: 80, align: "center" },
            { field: 'State', title: '状态', minWidth: 80, align: "center", templet: '#State' },
            { title: '操作', minWidth: 120, templet: '#managerListBar', fixed: "right", align: "center" }
        ]],
        totalRow: true  //开启合计行
    });

    table.on('tool(managerList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        console.log(data);
        if (layEvent === 'audit') { //审核并打款
            var msg = '您确认将【' + data.UserRealName + '(' + data.UserName + ')】的 <br />【$' + data.Money + '】美元直接打入第三方系统吗？';
            layer.confirm(msg, { icon: 3, title: '入金审核' }, function (index) {
                //获取防伪标记
                audit(data); 
            });
        } else if (layEvent === 'nosubmit') { //未提交订单
            var msg = '您确认将【' + data.UserRealName + '(' + data.UserName + ')】未提交的订单 <br />【$' + data.Money + '】美元直接打入第三方系统吗？';
            layer.confirm(msg, { icon: 3, title: '入金审核' }, function (index) {
                //获取防伪标记
                audit(data);
            });
        } else if (layEvent === 'cancel') { //取消此订单
            layer.confirm('您确认取消用户入金审核？', { icon: 3, title: '入金审核' }, function (index) {
                //获取防伪标记
                cancel(data);
            });
        }
    });

    function audit(item) {
        //console.log(item);
        $.ajax({
            type: 'POST',
            url: '/InOutCash/AuditInCoin/?inCoinId=' + item.Id,
            data: {},
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                var index = layer.msg(data.ResultMsg, {
                    time: 2000 //20s后自动关闭
                }, function () {
                    tableIns.reload();
                    layer.close(index);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }

    function cancel(item) {
        $.ajax({
            type: 'POST',
            url: '/InOutCash/CancelInCoin/?inCoinId=' + item.Id,
            data: {},
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                var index = layer.msg(data.ResultMsg, {
                    time: 2000 //20s后自动关闭
                }, function () {
                    tableIns.reload();
                    layer.close(index);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        //if ($(".searchVal").val() !== '') {
        table.reload("managerListTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                key: $(".searchVal").val(),  //搜索的关键字
                state: $(".searchState").val(),
                start: $(".startdate").val(),
                end: $(".enddate").val(),
                managerId: $(".searchManagerId").val()
            }
        });
        //} else {
        //    layer.msg("请输入搜索的内容");
        //}
    });

    loadDate();

});